package cvo.chess.client.entities.armies.pieces;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cvo.chess.client.entities.IGameFacade;
import cvo.chess.client.entities.chessboard.ChessboardCoordinate;
import cvo.chess.client.entities.chessboard.IChessboardCoordinate;
import cvo.chess.client.entities.game.Army;
import cvo.chess.client.entities.game.IChessboard;
import cvo.chess.client.entities.game.IPiece;
import cvo.chess.client.entities.game.Piece;
import cvo.chess.client.util.BoardUtil;


/**
 * 
 * @author Ricardo Cappelle
 *
 */

public class Knight extends Piece {
	
	enum DIRECTION {UP_RIGHT, RIGHT_UP, RIGHT_DOWN, DOWN_RIGHT, DOWN_LEFT, LEFT_DOWN, LEFT_UP, UP_LEFT};
	
	public Knight(IGameFacade gf, Army army){
		super(gf, army);
	}
	
	public Knight(IGameFacade gf, Army army, String name) {
		super(gf, army,name);
	}

	@Override
	public List<ChessboardCoordinate> getPossibleMoves() throws Exception {
		/*============================================================
		 Variables 
		============================================================*/
		
		List<ChessboardCoordinate> possibleMoves = new ArrayList<ChessboardCoordinate>();
		IChessboardCoordinate currentPosition = gf.getPositionOfPiece(this);
	
		possibleMoves.add(getPostition(currentPosition, DIRECTION.UP_RIGHT));
		possibleMoves.add(getPostition(currentPosition, DIRECTION.RIGHT_UP));
		possibleMoves.add(getPostition(currentPosition, DIRECTION.RIGHT_DOWN));
		possibleMoves.add(getPostition(currentPosition, DIRECTION.DOWN_RIGHT));
		possibleMoves.add(getPostition(currentPosition, DIRECTION.DOWN_LEFT));
		possibleMoves.add(getPostition(currentPosition, DIRECTION.LEFT_DOWN));
		possibleMoves.add(getPostition(currentPosition, DIRECTION.LEFT_UP));
		possibleMoves.add(getPostition(currentPosition, DIRECTION.UP_LEFT));
		// Remove null objects from the list
		possibleMoves.removeAll(Arrays.asList((Object)null));
		
		return possibleMoves;
	}
	
	/**
	 * Gets a coordinate for the position and validates that postion, if its not good or a friendly piece is in the way,
	 * it returns null;
	 * @param currentPosition The postion the piece is currently in
	 * @param direction The direction the knight needs to move
	 * @return A ChessboardCoordinate or a null
	 */
	private ChessboardCoordinate getPostition(IChessboardCoordinate currentPosition, DIRECTION direction){
		IChessboard board = gf.getBoard();
		Integer column = currentPosition.getCol();
		Integer row = currentPosition.getRow();
		
	
		
		//Clockwise
		
		if(direction.equals(DIRECTION.UP_RIGHT)){
			row = row-2;
			column = column+1;
		} else if(direction.equals(DIRECTION.RIGHT_UP)){
			row = row-1;
			column = column+2;
		} else if(direction.equals(DIRECTION.RIGHT_DOWN)){
			row = row+1;
			column = column+2;
		} else if(direction.equals(DIRECTION.DOWN_RIGHT)){
			row = row+2;
			column = column+1;
		} else if(direction.equals(DIRECTION.DOWN_LEFT)){
			row = row+2;
			column = column-1;
		} else if(direction.equals(DIRECTION.LEFT_DOWN)){
			row = row+1;
			column = column-2;
		} else if(direction.equals(DIRECTION.LEFT_UP)){
			row = row-1;
			column = column-2;
		} else if(direction.equals(DIRECTION.UP_LEFT)){
			row = row-2;
			column = column-1;
		}
		
		ChessboardCoordinate newCoordinate = new ChessboardCoordinate(board, column, row);
		
		
		//See if position is on board
		if(!BoardUtil.isValidPositionOnBoard(board,newCoordinate)){
			return null;
		}
		
		
		//Check if there is a piece at the new position
		IPiece posseblePieceAtLocation = gf.getPieceAt(newCoordinate.getA1Notation());
		
		if(posseblePieceAtLocation == null){
			return newCoordinate;
		} else if(!posseblePieceAtLocation.getColor().equals(this.getColor())){
			//If colors differ, it is an enemy so postition my be taken
			return newCoordinate; 
		}	
		
		//Its friendly an position may not be taken
		return null;	
		
		
		
		

		
	}


}